﻿package com.ouken.activity;




import java.util.ArrayList;

import com.ouken.tool.AvailableSSIDListAdapter;
import com.ouken.tool.Device;
import com.ouken.tool.DeviceslistAdapter;

import device_ctr.Device_ctr;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class AddDeviceActivity extends Activity {
	private Context context;
	private ImageButton back2;
	private ImageButton back1;
	private ListView available_SSID_listView;
	private ArrayList<Device> availableSSIDLists;
	private String[] contents ;/* {"deviceSSID","devicePWD","signalstatus",
			                     "QZ0112345678","123456","1",
			                     "QZ0212345678","123456","1",
			                     "QZ0312345678","123456","1",
			                     "QZ0412345678","123456","1",
			                     "QZ0581345678","123456","1",
			                     "QZ0612345678","123456","1",
			                     "QZ0712345678","123456","1",
			                     "QZ0812345678","123456","1",
			                     "QZ0912345678","123456","1",
			                     "QZ1012345678","123456""1"}*/
	private String result =null;
	private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
         public void run () {
        	 updateDeviceList();
         handler.postDelayed(this,2500);
      }
    };
	private void initView(){
		context = this;
		back1 = (ImageButton)findViewById(R.id.add_device_back1); 
		back2 = (ImageButton)findViewById(R.id.add_device_back2); 
		available_SSID_listView = (ListView)findViewById(R.id.available_ssid_listView);
		availableSSIDLists = new ArrayList<Device>();
	}
	private void updateDeviceList(){
		int dataLength;
		Device deviceTmp;
		Device_ctr kk=new Device_ctr(context);
		result=kk.get_all_net();
		if(result == null) return;
		
		availableSSIDLists.clear();
		
		contents = result.split(",");
		dataLength = contents.length/3;
		
		for(int i =0; i<dataLength; i++){
		  deviceTmp = new Device(contents[i*3],null,Integer.parseInt(contents[i*3+2]),"1",(contents[i*3+1]));
		  availableSSIDLists.add(deviceTmp);
		}
		
		AvailableSSIDListAdapter available_ssid_adapter=new AvailableSSIDListAdapter(this,availableSSIDLists);  
		available_SSID_listView.setAdapter(available_ssid_adapter);
		
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_device);
		initView();
		
		updateDeviceList();
		//*******************************************************
		handler.removeCallbacks(runnable);
		handler.postDelayed(runnable,2500);
		//*******************************************************
		back1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				AddDeviceActivity.this.finish();
			}
		});
		
		back2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				AddDeviceActivity.this.finish();
			}
		});
		
		available_SSID_listView.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(android.widget.AdapterView<?> arg0,
					android.view.View arg1, int arg2, long arg3) {
				
				
				Device_ctr jj = new Device_ctr(context);
				//jj.AddWifiInfo(availableSSIDLists.get(arg2).getDeviceSSID().toString(),"12345678",3);
				int result=jj.Connect_wifi(availableSSIDLists.get(arg2).getDeviceSSID().toString());
				Log.e("Duan",context.getString(result) );
				if (result==R.string.wifi_Ok){
					Intent intent = new Intent(context, AddDeviceInputPWDActivity.class); 
				    intent.putExtra("deviceSSID",availableSSIDLists.get(arg2).getDeviceSSID().toString());
				    intent.putExtra("deviceBSSID",availableSSIDLists.get(arg2).getDeviceSSID().toString());
				    intent.putExtra("devicePWD",availableSSIDLists.get(arg2).getDevicePWD());
				    intent.putExtra("deviceName",availableSSIDLists.get(arg2).getDeviceName());
				    startActivity(intent);
				}
				else{
						AlertDialog myDialog = new AlertDialog.Builder(AddDeviceActivity.this).
			        	setTitle(R.string.error).
			        	setMessage(context.getString(result)).
			        	setPositiveButton(R.string.confirm, null).create();
			        	myDialog.show();
				}
			}
			
		});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return true;
	}

}
